/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddProfileForm.java
 *
 * Created on Oct 17, 2011, 5:06:06 PM
 */
package project;

import Utils.Helper;
import Utils.checkValidate;
import beanControl.collateralControl;
import beanControl.customerControl;
import beanControl.loanProfileControl;
import beanInfo.collateral;
import beanInfo.customers;
import beanInfo.interest;
import beanInfo.loanProfile;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 *
 * @author Quynh Nhu
 */
public class AddProfileForm extends javax.swing.JFrame {

    loanProfileControl lpCtr = new loanProfileControl();
    customerControl custCtr = new customerControl();
    collateralControl colCtr = new collateralControl();
    customers cus = new customers();
    loanProfile loan = new loanProfile();
    collateral col = new collateral();
    String username;
    String id = "";
    SelectInterest selectin;

    /** Creates new form AddProfileForm */
    public AddProfileForm(String ID) {
        initComponents();
        this.id = ID;
        this.username = ID;
        //  Helper.successMessage(id, "wanj");
        loadBranches();

    }

    public void SelectCus() {
    }

    private void loadBranches() {
        lpCtr.getListBrancheCBO(cboBranche);
    }

    public void insertCustomer() {
        cus.setCusName(txtCustName.getText());
        if (rdFemale.isSelected() == true) {
            cus.setGender("Female");
        } else {
            cus.setGender("Male");
        }
        cus.setBirthDay(Helper.ConvertDateToString(dbBirthday.getDate()));
        cus.setIdentification(txtIdentification.getText());
        cus.setIssueDate(Helper.ConvertDateToString(dbIssueDate.getDate()));

        cus.setPlaceSupply(txtPlaceSupply.getText());
        cus.setStatus("No");
        cus.setBranchesID(Integer.parseInt(cboBranche.getSelectedItem().toString().split("-")[1]));
        custCtr.insertCustomer(cus);
    }

    public void insertLoanProfile() {
        loan.setDateLoan(Helper.ConvertDateToString(new java.util.Date()));
        loan.setCusID(lpCtr.getMaxZCusID());
        loan.setUserName(username);
        loan.setInterestID(Integer.parseInt(txtin.getText()));
        loan.setLimitInterset(Float.parseFloat(txtInterest.getText()));
        loan.setStatus("False");
        loan.setTotalAmount(Float.parseFloat(txtAmount.getText()));
        lpCtr.insertLoanProfile(loan);

    }

    public void insertCollatera() {
        col.setCollateralName(txtCollaName.getText());
        col.setQuantity(Integer.parseInt(txtQuantity.getText()));
        col.setValuesAsset(txtValueAsset.getText());
        col.setFeatures(txtFeatures.getText());
        col.setProfileID(colCtr.getMaxZprofileID());
        colCtr.insertCollateral(col);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtCustName = new javax.swing.JTextField();
        rdFemale = new javax.swing.JRadioButton();
        rdMale = new javax.swing.JRadioButton();
        txtPlaceSupply = new javax.swing.JTextField();
        cboBranche = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        txtAmount = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        txtYear = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtInterest = new javax.swing.JTextField();
        txtIdentification = new javax.swing.JTextField();
        btnsave = new javax.swing.JButton();
        dbBirthday = new com.toedter.calendar.JDateChooser();
        dbIssueDate = new com.toedter.calendar.JDateChooser();
        txtin = new javax.swing.JTextField();
        btnSelect = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        txtCollaName = new javax.swing.JTextField();
        txtQuantity = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        txtValueAsset = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtFeatures = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        tblCollateral = new javax.swing.JTable();

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setLayout(null);

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));
        jPanel1.setLayout(null);

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setLayout(null);

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));
        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Customer"));
        jPanel4.setLayout(null);

        jLabel2.setText("CustName");
        jPanel4.add(jLabel2);
        jLabel2.setBounds(20, 30, 60, 14);

        jLabel3.setText("Gender");
        jPanel4.add(jLabel3);
        jLabel3.setBounds(20, 60, 60, 14);

        jLabel4.setText("Birthday");
        jPanel4.add(jLabel4);
        jLabel4.setBounds(20, 100, 60, 14);

        jLabel5.setText("Identification");
        jPanel4.add(jLabel5);
        jLabel5.setBounds(20, 140, 70, 14);

        jLabel6.setText("IssueDate");
        jPanel4.add(jLabel6);
        jLabel6.setBounds(430, 30, 80, 20);

        jLabel7.setText("PlaceSupply");
        jPanel4.add(jLabel7);
        jLabel7.setBounds(430, 70, 70, 20);

        jLabel8.setText("TotalAmount");
        jPanel4.add(jLabel8);
        jLabel8.setBounds(430, 150, 80, 20);
        jPanel4.add(txtCustName);
        txtCustName.setBounds(110, 20, 200, 30);

        buttonGroup1.add(rdFemale);
        rdFemale.setSelected(true);
        rdFemale.setText("Female");
        jPanel4.add(rdFemale);
        rdFemale.setBounds(110, 60, 90, 23);

        buttonGroup1.add(rdMale);
        rdMale.setText("rdMale");
        jPanel4.add(rdMale);
        rdMale.setBounds(220, 60, 70, 23);
        jPanel4.add(txtPlaceSupply);
        txtPlaceSupply.setBounds(510, 60, 190, 30);

        cboBranche.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPanel4.add(cboBranche);
        cboBranche.setBounds(510, 100, 190, 30);

        jLabel9.setText("Interest");
        jPanel4.add(jLabel9);
        jLabel9.setBounds(20, 180, 60, 20);

        jLabel10.setText("Branche");
        jPanel4.add(jLabel10);
        jLabel10.setBounds(430, 100, 60, 20);
        jPanel4.add(txtAmount);
        txtAmount.setBounds(510, 140, 190, 30);

        jLabel11.setText("Years");
        jPanel4.add(jLabel11);
        jLabel11.setBounds(430, 180, 60, 14);

        txtYear.setEditable(false);
        jPanel4.add(txtYear);
        txtYear.setBounds(510, 180, 60, 30);

        jLabel12.setText("Interest");
        jPanel4.add(jLabel12);
        jLabel12.setBounds(580, 180, 50, 14);

        txtInterest.setEditable(false);
        jPanel4.add(txtInterest);
        txtInterest.setBounds(640, 180, 60, 30);
        jPanel4.add(txtIdentification);
        txtIdentification.setBounds(110, 130, 207, 30);

        btnsave.setBackground(new java.awt.Color(0, 0, 0));
        btnsave.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnsave.setForeground(new java.awt.Color(255, 255, 255));
        btnsave.setText("Save");
        btnsave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsaveActionPerformed(evt);
            }
        });
        jPanel4.add(btnsave);
        btnsave.setBounds(110, 210, 65, 30);

        dbBirthday.setDateFormatString("MM/dd/yyyy");
        jPanel4.add(dbBirthday);
        dbBirthday.setBounds(110, 90, 210, 30);

        dbIssueDate.setDateFormatString("MM/dd/yyyy");
        jPanel4.add(dbIssueDate);
        dbIssueDate.setBounds(510, 20, 190, 30);

        txtin.setEnabled(false);
        jPanel4.add(txtin);
        txtin.setBounds(110, 170, 210, 30);

        btnSelect.setText("Select");
        btnSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectActionPerformed(evt);
            }
        });
        jPanel4.add(btnSelect);
        btnSelect.setBounds(330, 170, 61, 23);

        jPanel3.add(jPanel4);
        jPanel4.setBounds(10, 40, 740, 250);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel1.setForeground(new java.awt.Color(204, 0, 0));
        jLabel1.setText("Add profile customer");
        jPanel3.add(jLabel1);
        jLabel1.setBounds(200, 10, 249, 29);

        jPanel5.setBackground(new java.awt.Color(255, 255, 255));
        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Add Collateral", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 14), java.awt.Color.blue)); // NOI18N
        jPanel5.setLayout(null);

        jLabel13.setText("CollateralName");
        jPanel5.add(jLabel13);
        jLabel13.setBounds(16, 36, 72, 14);

        jLabel14.setText("Quantity");
        jPanel5.add(jLabel14);
        jLabel14.setBounds(16, 81, 42, 14);
        jPanel5.add(txtCollaName);
        txtCollaName.setBounds(112, 30, 184, 27);
        jPanel5.add(txtQuantity);
        txtQuantity.setBounds(112, 75, 184, 27);

        jLabel15.setText("ValuesAsset");
        jPanel5.add(jLabel15);
        jLabel15.setBounds(16, 126, 58, 14);
        jPanel5.add(txtValueAsset);
        txtValueAsset.setBounds(112, 120, 184, 27);

        jLabel16.setText("Features");
        jPanel5.add(jLabel16);
        jLabel16.setBounds(16, 165, 43, 14);

        txtFeatures.setColumns(20);
        txtFeatures.setRows(5);
        jScrollPane1.setViewportView(txtFeatures);

        jPanel5.add(jScrollPane1);
        jScrollPane1.setBounds(110, 160, 184, 73);

        jButton1.setBackground(new java.awt.Color(0, 0, 0));
        jButton1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jButton1.setForeground(new java.awt.Color(255, 255, 255));
        jButton1.setText("Save");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel5.add(jButton1);
        jButton1.setBounds(112, 244, 90, 30);

        jPanel3.add(jPanel5);
        jPanel5.setBounds(10, 300, 340, 290);

        jPanel6.setBackground(new java.awt.Color(255, 255, 255));
        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "List Collateral", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 14), new java.awt.Color(0, 0, 204))); // NOI18N

        tblCollateral.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane5.setViewportView(tblCollateral);

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel3.add(jPanel6);
        jPanel6.setBounds(360, 310, 400, 270);

        jPanel1.add(jPanel3);
        jPanel3.setBounds(10, 11, 770, 600);

        jPanel2.add(jPanel1);
        jPanel1.setBounds(10, 10, 790, 630);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 813, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 632, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:

    if (txtCollaName.getText().length() == 0) {
        Helper.successMessage("Enter collName", "Warning");

    } else if (txtQuantity.getText().length() == 0) {
        Helper.successMessage("Enter quantity", "Warning");

    } else if (txtValueAsset.getText().length() == 0) {
        Helper.successMessage("Enter ValueAsset", "Warning");

    } else if (txtFeatures.getText().length() == 0) {
        Helper.successMessage("Enter Features", "Warning");

    } else {
        insertCollatera();
    }

}//GEN-LAST:event_jButton1ActionPerformed

private void btnsaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsaveActionPerformed
// TODO add your handling code here:

    if (txtCustName.getText().length() == 0) {
        Helper.successMessage("Enter CustName", "Warning");

    } else if (String.valueOf(dbBirthday.getDate()).length() == 0) {
        Helper.successMessage("Choose birthday", "Warning");

    } else if (txtIdentification.getText().length() == 0) {
        Helper.successMessage("Enter identtifi", "Warning");

    } else if (checkValidate.checkFloatNumber(txtAmount.getText()) == false) {
        Helper.successMessage("TotalAmount is number", "wanning");
    } else if (checkValidate.checkNumber(txtIdentification.getText()) == false) {
        Helper.successMessage("Identification is number", "wanning");
    } else if (String.valueOf(txtin.getText()).length() == 0) {
        Helper.successMessage("Choose Interest", "waning");
    } else if (String.valueOf(dbIssueDate.getDate()).length() == 0) {
        Helper.successMessage("Select IssueDate", "Warning");


    } else if (txtPlaceSupply.getText().length() == 0) {
        Helper.successMessage("Enter placeSupply", "Warning");
        // txtPlaceSupply.requestFocus();


    } else if (txtAmount.getText().length() == 0) {
        Helper.successMessage("Enter Total Amount", "Warning");
        // txtAmount.requestFocus();


        if (txtCustName.getText().length() == 0) {
            Helper.successMessage("Enter CustName", "Warning");



        } else if (txtAmount.getText().length() == 0) {
            Helper.successMessage("Enter Total Amount", "Warning");
            // txtAmount.requestFocus();
        } else if (Helper.isValidDate(Helper.ConvertDateToString(dbBirthday.getDate())) == false) {
            Helper.successMessage("birthday invalid", " wanning");

        } else if (Helper.isValidDate(Helper.ConvertDateToString(dbIssueDate.getDate())) == false) {
            Helper.successMessage("birthday invalid", " wanning");

        }


    } else {

        insertCustomer();
        insertLoanProfile();
    }
}//GEN-LAST:event_btnsaveActionPerformed

private void btnSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelectActionPerformed
    selectin = new SelectInterest(null, true, txtin, txtInterest, txtYear);
    selectin.setVisible(true);// TODO add your handling code here:

}//GEN-LAST:event_btnSelectActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(AddProfileForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(AddProfileForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(AddProfileForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(AddProfileForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                new AddProfileForm().setVisible(true);
//
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSelect;
    private javax.swing.JButton btnsave;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cboBranche;
    private com.toedter.calendar.JDateChooser dbBirthday;
    private com.toedter.calendar.JDateChooser dbIssueDate;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JRadioButton rdFemale;
    private javax.swing.JRadioButton rdMale;
    private javax.swing.JTable tblCollateral;
    private javax.swing.JTextField txtAmount;
    private javax.swing.JTextField txtCollaName;
    private javax.swing.JTextField txtCustName;
    private javax.swing.JTextArea txtFeatures;
    private javax.swing.JTextField txtIdentification;
    private javax.swing.JTextField txtInterest;
    private javax.swing.JTextField txtPlaceSupply;
    private javax.swing.JTextField txtQuantity;
    private javax.swing.JTextField txtValueAsset;
    private javax.swing.JTextField txtYear;
    private javax.swing.JTextField txtin;
    // End of variables declaration//GEN-END:variables
}
